import { GameData } from "../Script/Data/GameData";

const { ccclass, property, executionOrder } = cc._decorator;

@ccclass
export default class Grid_BJQ extends cc.Component {
    /** 选中框节点 */
    @property(cc.Node)
    select: cc.Node = null;
    
    id: number = 0;
    col: number = 0;
    row: number = 0;

    isSelect = false;

    // 初始化
    init(_id: number, _col: number, _row: number) {
        this.id = _id;
        this.col = _col;
        this.row = _row;
      
        this.initWidthAndHeight(GameData.cell_width, GameData.cell_height);
        this.InitPosition();

        this.Refresh();

        this.AddListener();
    }

    GetNodeWorldPostation(_node: cc.Node): cc.Vec3 {
        let pos: cc.Vec3 = cc.v3();
        if (_node.parent == null) {
            pos = cc.v3(_node.getPosition());
        } else {
            pos = cc.v3(_node.parent.convertToWorldSpaceAR(_node.getPosition()));
        }
        return pos;
    }

    TOUCH_END() {
        // GameData.IsMouseDown = false;
    }

    async AddListener() {
        this.node.on(
            cc.Node.EventType.TOUCH_END,
            (t) => {
                this.TOUCH_END();
            },
            this
        );
        this.node.on(
            cc.Node.EventType.TOUCH_CANCEL,
            (t) => {
                this.TOUCH_END();
            },
            this
        );
        // this.node.on(cc.Node.EventType.TOUCH_MOVE, (t) => { }, this);

        this.node.on(cc.Node.EventType.TOUCH_START, async (t) => {
            // console.log(`TOUCH_START`);
            // console.log(`col-row`, this.col, this.row);

            // this.AllGridSelect(false);
            // this.Select(true);
        }, this);
    }

    Set(_type) {
        if (_type === null) {
            this.清除类型();
        }
       

        this.Refresh();
    }

    清除类型() {
    }

    选择后() {
    }

    Refresh() {
        
    }

    Select(select: boolean): void {
        this.isSelect = select;
        this.select.active = select;
    }


    GetGrid(x: number, y: number) {
        if (GameData.arrGrids[x] && GameData.arrGrids[x][y]) {
            return GameData.arrGrids[x][y];
        }
        return null;
    }

    // 初始化单元宽高
    initWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.width = cardWidth;
        if (cardHeight) this.node.height = cardHeight;
    }

    // 初始化位置
    InitPosition() {
        // let x = CardFunctions.Get_x_by_col(this.col);
        // let y = CardFunctions.Get_y_by_row(this.row);
        // this.node.position = cc.v3(x, y, 0);
    }

    RemoveBlock() {
        // var index = GameData.arrBlocks.indexOf(this.block);
        // if (index !== -1) {
        //     GameData.arrBlocks.splice(index, 1);
        // }

        // // this.block.node.destroy();
        // this.block = null;
    }
}